package com.ironone.streaming;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ironone.streaming.application.CusAlrtBtnTypes;
import com.ironone.streaming.application.CusAlrtIconTypes;
import com.ironone.streaming.application.CustomAlertDialog;
import com.ironone.streaming.application.FormObject;
import com.ironone.streaming.lite.R;
import com.ironone.streaming.model.NewsDataModel;

/**
 * { News fragment shows news in the rss feed to the user}
 * 
 * @Created on Dec 01, 2014
 * @author Helani
 */
public class NewsFragment extends Fragment {
	View view;
	SlidingPaneLayout mPane;
	Button btnMenu;

	ImageView albumart_bg;
	Bitmap bitmap;

	// Custom view: custom_imageview.xml
	TextView text_display, text_link;
	View image_view_overlay;

	LinearLayout image_container, background_color_layer;
	ImageView logo;

	/*
	 * @Override public void onCreate(Bundle savedInstanceState) { try {
	 * super.onCreate(savedInstanceState);
	 * Thread.setDefaultUncaughtExceptionHandler(new
	 * ExceptionHandler(getActivity()));
	 * 
	 * } catch (Exception e) { CustomAlertDialog alertDialog = new
	 * CustomAlertDialog(getActivity(), CusAlrtBtnTypes.OK,
	 * CusAlrtIconTypes.DUCK);
	 * alertDialog.setTitleText(getString(R.string.what_the_duck_));
	 * alertDialog.setMessageText(getString(R.string.something_snapped));
	 * alertDialog.show(); } }
	 */

	@Override
	public View onCreateView(LayoutInflater inflater,
			final ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.news_fragment, container, false);

		try {
			mPane = (SlidingPaneLayout) getActivity().findViewById(R.id.pane);
			btnMenu = (Button) view.findViewById(R.id.btnMenu);
			btnMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mPane.openPane();
				}
			});

			// Menu btn
			int scrnWidth = FormObject.getInstance().getScreenWidth();
			int supWidth = (int) ((scrnWidth * 0.6));
			RelativeLayout.LayoutParams abParam = (RelativeLayout.LayoutParams) btnMenu
					.getLayoutParams();
			abParam.width = (int) (supWidth - (supWidth * 0.88));
			abParam.height = (int) (supWidth - (supWidth * 0.88));
			btnMenu.setLayoutParams(abParam);

			// Logo
			logo = (ImageView) view.findViewById(R.id.imageView1);
			RelativeLayout.LayoutParams logoParam = (RelativeLayout.LayoutParams) logo
					.getLayoutParams();
			logoParam.width = (int) (supWidth - (supWidth * 0.62));
			logoParam.height = (int) (supWidth - (supWidth * 0.62));
			logo.setLayoutParams(logoParam);

			// ListView
			final CustomArrayAdapter adapter = new CustomArrayAdapter(
					getActivity(), MainMenuFragment.newsList);
			ListView NewsListView = (ListView) view
					.findViewById(R.id.newsListView);
			NewsListView.setAdapter(adapter);

			// Open Browser on list item clicked
			NewsListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> list, View view,
						int position, long id) {

					try {

						NewsDataModel item = (NewsDataModel) adapter
								.getItem(position);
						Uri uriUrl = Uri.parse(item.getLink());
						Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
								uriUrl);
						// launchBrowser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getActivity().startActivity(launchBrowser);

						// getActivity().finish();

					} catch (Exception e) {
						//String fsf = e.getMessage();
					}
				}
			});

		} catch (Exception e) {
			CustomAlertDialog alertDialog = new CustomAlertDialog(
					getActivity(), CusAlrtBtnTypes.OK, CusAlrtIconTypes.DUCK);
			alertDialog.setTitleText(getString(R.string.what_the_duck_));
			//alertDialog.setMessageText(getString(R.string.something_snapped));
			alertDialog.show();
		}
		return view;
	}

	// custom adapter for list view
	public class CustomArrayAdapter extends ArrayAdapter<NewsDataModel> {
		private final Context context;
		private final List<NewsDataModel> values;

		public CustomArrayAdapter(Context context, List<NewsDataModel> values) {
			super(context, R.layout.news_list_row, values);
			this.context = context;
			this.values = values;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowView = inflater.inflate(R.layout.news_list_row, parent,
					false);

			ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

			// if image is in drawable folder
			// imageView.setImageResource(R.drawable.news);

			// if image is online
			if (values.get(position).getImage() != null) {
				imageView.setImageBitmap(values.get(position).getImage());
			} else {
				imageView.setImageResource(R.drawable.news);
			}

			String des = values.get(position).getDescription();
			if (isDataAttachedtoDes(des)) {
				des = split(des);
			}
			// String desplay = des.substring(0, 40);
			String desplay = des;
			if (values.get(position).isEncoded()) {
				Typeface type = Typeface.createFromAsset(getActivity()
						.getAssets(), "ARADANA.TTF");
				TextView textView1 = (TextView) rowView
						.findViewById(R.id.title);
				textView1.setTypeface(type);
				textView1.setText(values.get(position).getTitle());
				TextView textView2 = (TextView) rowView.findViewById(R.id.des);
				textView2.setTypeface(type);
				// Button btn1=(Button) rowView.findViewById(R.id.btn11);
				// btn1.setTypeface(type);
				// String des = values.get(position).getDescription();
				// String desplay = des.substring(0, 40);
				textView2.setText(desplay);
				// btn1.setText(desplay+"'''");

			} else {
				TextView textView1 = (TextView) rowView
						.findViewById(R.id.title);
				TextView textView2 = (TextView) rowView.findViewById(R.id.des);
				// Button btn1=(Button) rowView.findViewById(R.id.btn11);
				textView1.setText(values.get(position).getTitle());
				// String des = values.get(position).getDescription();
				// String desplay = split(des).substring(0, 40);
				textView2.setText(desplay);
				// btn1.setText(desplay + "...");
			}

			return rowView;
		}
	}

	public String split(String splitString) {
		String delims = ">";
		String[] tokens = splitString.split(delims);
		// int tokenCount = tokens.length;
		return tokens[1];
	}

	public boolean isDataAttachedtoDes(String text) {
		if (text.contains(">")) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * private void resetUi(View vx){ int childcount =
	 * image_container.getChildCount(); for (int i=0; i < childcount; i++){ View
	 * v = image_container.getChildAt(i);
	 * 
	 * if(v.equals(vx)) continue;
	 * 
	 * TextView tv = (TextView) v.findViewById(R.id.text_display);
	 * //.findViewById(R.id.text_display); TextView tl = (TextView)
	 * v.findViewById(R.id.text_link); image_view_overlay = (View)
	 * v.findViewById(R.id.image_view_overlay);
	 * 
	 * if(tv.getVisibility() == View.VISIBLE){ tv.setVisibility(View.INVISIBLE);
	 * tl.setVisibility(View.INVISIBLE);
	 * image_view_overlay.setVisibility(View.INVISIBLE); } } }
	 */
}